﻿Imports System.IO
Imports System.Net.Mail
''' <summary>
''' This class is used for sending email via Exchange or SMTP Server
''' </summary>
''' <remarks></remarks>
Public Class EmailHelper
    Private EmailClient As SmtpClient

    ''' <summary>
    ''' Constructor with Server Name
    ''' </summary>
    ''' <param name="_EmaiLServer">Email Server Name</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal _EmaiLServer As String)
        EmailClient = New SmtpClient
        EmailClient.Host = _EmaiLServer
    End Sub

    Public Sub New()
        EmailClient = New SmtpClient
        EmailClient.Host = ""
    End Sub
    ''' <summary>
    ''' Get/Set Email Host information
    ''' </summary>
    ''' <value> Set Email Host information</value>
    ''' <returns>Get Email Host Information</returns>
    ''' <remarks></remarks>
    Public Property EmailHost()
        Get
            Return EmailClient.Host
        End Get
        Set(ByVal value)
            EmailClient.Host = value
        End Set
    End Property

    Public Function SendEmail(ByVal EmailAddressFrom As String, ByVal EmailFrom As String,
                              ByVal EmailAddressTo As String, ByVal CCAddress As String, ByVal BCCAddress As String,
                              ByVal Subject As String, ByVal Content As String, Optional ByVal isHTML As Boolean = True,
                              Optional ByVal FileToAttach As List(Of String) = Nothing, Optional ByVal EmailUsername As String = "", Optional ByVal EmailPassword As String = "",
                              Optional ByVal ReplyTo As String = ""
                           ) As Boolean
        Dim result As Boolean
        Dim msg As New MailMessage
        If EmailAddressTo Is Nothing Then
            EmailAddressTo = ""
        End If
        If CCAddress Is Nothing Then
            CCAddress = ""
        End If
        If BCCAddress Is Nothing Then
            BCCAddress = ""
        End If


        Try
            msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
            If Not String.IsNullOrEmpty(EmailFrom) Then
                msg.From = New MailAddress(EmailAddressFrom, EmailFrom)
            Else
                msg.From = New MailAddress(EmailAddressFrom)
            End If

            Dim TempToAddressArray() As String = EmailAddressTo.Split(";")
            Dim TempCCAddressArray() As String = CCAddress.Split(";")
            Dim TempBCCAddressArray() As String = BCCAddress.Split(";")


            For t As Int32 = 0 To TempToAddressArray.Length - 1
                If (TempToAddressArray(t).Trim.Length > 0) Then
                    msg.To.Add(New MailAddress(TempToAddressArray(t).Trim))
                End If
            Next

            For c As Int32 = 0 To TempCCAddressArray.Length - 1
                If (TempCCAddressArray(c).Trim.Length > 0) Then
                    msg.CC.Add(New MailAddress(TempCCAddressArray(c).Trim))
                End If
            Next

            For b As Int32 = 0 To TempBCCAddressArray.Length - 1
                If (TempBCCAddressArray(b).Trim.Length > 0) Then
                    msg.Bcc.Add(New MailAddress(TempBCCAddressArray(b).Trim))
                End If
            Next

            If (Not String.IsNullOrEmpty(ReplyTo)) Then
                Dim TempReplyToArray() As String = ReplyTo.Split(";")
                For r As Integer = 0 To TempReplyToArray.Count - 1
                    msg.ReplyToList.Add(New MailAddress(TempReplyToArray(r).Trim()))
                Next

            End If

            msg.Subject = Subject
            msg.Body = Content
            msg.IsBodyHtml = isHTML


            If Not FileToAttach Is Nothing Then
                For i As Int32 = 0 To FileToAttach.Count - 1

                    If File.Exists(FileToAttach(i)) Then
                        Dim attach As New Attachment(FileToAttach(i))
                        msg.Attachments.Add(attach)
                    End If
                Next
            End If

            If (Not String.IsNullOrEmpty(EmailUsername)) Then
                EmailClient.Credentials = New Net.NetworkCredential(EmailUsername, EmailPassword)
            End If
            EmailClient.Send(msg)

            result = True
        Catch ex As Exception
            result = False
        End Try


        Return result
    End Function

End Class
